package inventory.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import inventory.common.ResultData;
import inventory.entity.Repository;
import inventory.entity.Role;
import inventory.entity.User;
import inventory.entity.po.RolePo;
import inventory.mappers.RepositoryMapper;
import inventory.mappers.RoleMapper;
import inventory.mappers.UserMapper;
import inventory.service.RoleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import inventory.utils.UUIDUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sxt
 * @since 2019-11-06
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {


    @Autowired
    RoleMapper roleMapper;
    @Resource
    RepositoryMapper repositoryMapper;
    @Resource
    UserMapper userMapper;
    @Override
    public ResultData getSubRolesByManagerId(String managerId) {
        Role role = roleMapper.selectSubCityRoleByManagerId(managerId);
        List<RolePo> countyRoles = roleMapper.selectSubCountyRolesByRoleId(role.getRoleId());
        for (RolePo role1:countyRoles){
            List<Role> roleList = roleMapper.selectSubCountyEmployeeRolesByRoleId(role1.getRoleId());
            role1.setRoleList(roleList);
        }
        HashMap<String, String> city = new HashMap<>();
        city.put("city",role.getRole());
        city.put("cityRoleId",role.getRoleId());

//        for (RolePo r:countyRoles){
//            List<Role> countyEmployeeRoles = roleMapper.selectList(new EntityWrapper<Role>().eq("pid", r.getRoleId()));
//            r.setRoleList(countyEmployeeRoles);
//        }

        return new ResultData().OK().setObjs(countyRoles,city);
    }

    @Override
    public ResultData addSubCountyRoleAndSubCountyRepositoryByCityRoleId(String cityRoleId,String roleCounty) {

            String roleId = UUIDUtils.getId();//县区级roleId

            String state="1";
            roleMapper.insertCountyRole(roleId,roleCounty,cityRoleId,state);//增加县区级role

            String state2="0";
            String roleId2 = UUIDUtils.getId();//县区级员工roleId
            String roleCountyEmployee = roleCounty+"员工";
            roleMapper.insertCountyEmployeeRole(roleId2,roleCountyEmployee,roleId,state2);
            Repository repository = new Repository();
            repository.setRepositoryId(UUIDUtils.getId());
            repository.setRepositoryName(roleCounty+"的仓库");
            repository.setBelongingStatus("1");
            repository.setRoleId(roleId);
            repository.setUserId(null);
            repositoryMapper.insert(repository);


        return new ResultData().OK().setObjs("添加成功");
    }

    @Override
    public ResultData deleteCountyRoleAndCountyRepositoryByCountyRoleId(String countyRoleId) {

        List<Role> roles = roleMapper.selectSubCountyEmployeeRolesByRoleId(countyRoleId);
        for (Role role:roles){
            List<User> users = userMapper.selectUserByRoleId(role.getRoleId());//wode
            if(users.size()!=0){
                return new ResultData().FAILD().setObjs("对不起，该区县的下级员工未删除完全，" +
                        "故不能删除区县角色同时删除仓库");
            }else {
                repositoryMapper.deleteRepositoryByRoleId(countyRoleId);//删除县区库
                roleMapper.deleteRoleByRoleId(countyRoleId);
                roleMapper.deleteRoleByRoleId(role.getRoleId());
            }

        }

        return new ResultData().OK().setObjs("删除成功");
    }

    @Override
    public ResultData updateRoleByCountyRoleId(String countyRoleId,String role) {
        String roleId = countyRoleId;
        roleMapper.updateRoleByRoleId(role,roleId);
        List<Role> roleList = roleMapper.selectSubCountyEmployeeRolesByRoleId(roleId);
        for (Role r:roleList) {
            roleMapper.updateRoleByRoleId(role+"员工",r.getRoleId());
        }
        return new ResultData().OK().setObjs("修改成功");
    }
}
